/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package terminal.model;

import terminal.interfaces.ISerialPort;

/**
 * ISerialPort interface implementation that wraps the jssc library
 * @author P. Podwapinski
 */
public class SerialPort implements ISerialPort {
    
    /**
     * Constructor
     * @param serialPortDataProcessor the DataProcessor instance which will 
     * receive input values
     */
    public SerialPort( SerialPortDataProcessor serialPortDataProcessor )
    {
        setPortSettingsToDefault();
        m_serialPortDataListener = new SerialPortDataListener( null, serialPortDataProcessor );        
    }
    

    @Override public boolean open( final String portName, final String baudRate )
    {
        boolean result = false;

        m_serialPort = new jssc.SerialPort( portName );
        
        if ( null != m_serialPort )
        {            
            try
            {
                m_serialPort.openPort();
                
                final int baudRateNum = Integer.parseInt( baudRate );
                final int dataBits = jssc.SerialPort.DATABITS_8;
                final int stopBits = jssc.SerialPort.STOPBITS_1;
                final int parity = jssc.SerialPort.PARITY_NONE;
                
                if( m_serialPort.setParams( baudRateNum, dataBits, stopBits, parity ) )
                {
                    m_serialPortDataListener.setSerialPort( m_serialPort );

                    m_baudRate = baudRate;
                    result = true;
                }
            }
            catch( jssc.SerialPortException e )
            {
                System.out.println( e );
            }            
        }
        
        return result;
    }
    

    @Override public boolean close()
    {
        boolean result = false;
        
        if ( null != m_serialPort )
        {
            try
            {              
                if ( m_serialPort.closePort() )
                {
                    setPortSettingsToDefault();
                    result = true;
                }
            }
            catch( jssc.SerialPortException e )
            {
                System.out.println( e );
            }            
        }
        
        return result;
    }    
    
    
    @Override public boolean isOpened()
    {
        boolean result = false;
        
        if ( null != m_serialPort )
        {
            result = m_serialPort.isOpened();
        }
        
        return result;
    }
    

    @Override public boolean writeChar( char character )
    {
        boolean result = false;
        
        if ( null != m_serialPort )
        {
            try 
            {
                result = m_serialPort.writeByte( ( byte ) character );
            }
            catch( jssc.SerialPortException e )
            {
                
            }                           
        }
        
        return result;
    }
    
    
    @Override public final String getPortName()
    {
        String result = "";
        
        if ( null != m_serialPort )
        {
            result = m_serialPort.getPortName();
        }        
        
        return result;
    }
    

    @Override public final String getBaudrate()            
    {
        return m_baudRate;
    }
    
    
    /**
     * Resets the Port Settings to default values
     */
    private void setPortSettingsToDefault()
    {
        m_baudRate = "0";
        m_parity = "None";
        m_stopBits = "1";
        
        if ( !isOpened() )
        {
            m_serialPort = null;
        }
    }
      
    
    /**
     * Serial Port engine instance: jssc
     */
    jssc.SerialPort m_serialPort;
    
    /**
     * Internally used SerialPort Data Listener
     */
    SerialPortDataListener m_serialPortDataListener; 
    
    /**
     * String containing the BaudRate
     */
    String m_baudRate;
    
    /**
     * String containing the Parity setting
     */
    String m_parity;
    
    /**
     * String containing the Stop Bits setting
     */
    String m_stopBits;
}
